﻿
layui.use(['form','table','layer'], function(){
    //直接渲染表格
    var form = layui.form;
    var table = layui.table;

    var $ = layui.$;
    //添加类别
    $("#add_type").click(function(){
        layer.open({
            type: 1,
            area: ['500px','300px'],
            offset: '100px',
            title: '添加类别',
            content: $('#addTypeHtml').html(), // 注意这里有.html()
            success: function(layero, dIndex) {
                // 表单提交事件
                form.on('submit(modelSubmitAddType)', function(data) {
                    $.ajax({
                        type: "POST",
                        url: '/general/testzs/public/printing/Type/AddType',
                        data: data.field,
                        traditional: true,
                        dataType:"json",
                        success: function(data){
                            layer.alert(data.msg);
                            table.reload('demo', {})
                        }
                    })
                    return false;
                });
            },end : function() {
                //回调后结束操作
            }


        });
    });


    table.render({
        elem: '#demo'
        ,height: 312
        ,headers: {
            Accept: "application/json; charset=utf-8",
        }
        ,url: '/general/testzs/public/printing/Type/TypeList' //数据接口
        ,cols: [[ //表头
            {field: 'type_id', title: 'ID', width:80, sort: true, fixed: 'left'}
            ,{field: 'type_name', title: '类别名称', width:200}
            ,{field: 'order', title: '排序', width:80}
            ,{field: 'create_time', title: '创建时间', width:300}
            ,{field:'right', title: '操作',toolbar:"#barDemo"}
        ]]
        ,response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            , statusCode: 200 //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'count' //规定数据总数的字段名称，默认：count
            , dataName: 'data' //规定数据列表的字段名称，默认：data
        }
        ,toolbar:true
        ,page: true
        ,text: { none: '暂无相关数据' }
    });


    table.on('tool(test)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
                $.ajax({
                    type: "POST",
                    url: '/general/testzs/public/printing/Type/DelType',
                    data: {
                        'type_id':data.type_id
                    },
                    dataType:"json",
                    success: function(data){
                        if (data.code === 200){
                            layer.alert(data.msg);
                            obj.del();
                        }else{
                            layer.alert(data.msg);
                        }
                    }
                })

                layer.close(index);
            });
        } else if(obj.event === 'edit'){
            layer.open({
                type: 1,
                area: ['500px','300px'],
                offset: '100px',
                title: '修改'+data.type_name +'类别',
                content: $('#updateHtml').html(), // 注意这里有.html()
                success: function(layero, dIndex) {
                    $('#type_name').val(data.type_name);
                    $('#type_id').val(data.type_id);
                    $('#order').val(data.order);
                    // 表单提交事件
                    form.on('submit(modelSubmitEdit)', function(data) {
                        $.ajax({
                            type: "POST",
                            url: '/general/testzs/public/printing/Type/EditType',
                            data: data.field,
                            traditional: true,
                            dataType:"json",
                            success: function(data){
                                layer.alert(data.msg);
                                table.reload('demo', {})
                            }
                        })
                        return false;
                    });
                },end : function() {
                    //回调后结束操作
                }


            });


        }
    });

});

